#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"
#include "hi_adc.h"
#include "iot_errno.h"
//定义 ADC 相关硬件资源
#define LIGHT_SENSOR_CHAN_NAME HI_ADC_CHANNEL_3  //智能风扇红外传感通道
#define LED_PIN_NAME 6     // 智能风扇LED
#define MOTOR_PIN_NAME 2   // 智能风扇马达电机
static void ADCLightTask(void *arg)
{
    (void)arg; 
    while (1) {
    unsigned short data = 0;
    //读取 ADC 信息
    if (hi_adc_read(LIGHT_SENSOR_CHAN_NAME, &data, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0)== IOT_SUCCESS) {
        printf("ADC_VALUE = %d\n", (unsigned int)data);

        if(data > 130){
        
            IoTGpioSetOutputVal(LED_PIN_NAME,1);
            IoTGpioSetOutputVal(MOTOR_PIN_NAME,1);

        }else{

            IoTGpioSetOutputVal(LED_PIN_NAME,0);
            IoTGpioSetOutputVal(MOTOR_PIN_NAME,0);
        }

    osDelay(100);
    } 
    }
}
static void ADCLightDemo(void)
{
 osThreadAttr_t attr;
  /*LED 的 GPIO 初始化*/
 IoTGpioInit(LED_PIN_NAME);
 IoTGpioInit(MOTOR_PIN_NAME);

 IoTGpioSetFunc(LED_PIN_NAME, HI_IO_FUNC_GPIO_6_GPIO);
 IoTGpioSetFunc(MOTOR_PIN_NAME, HI_IO_FUNC_GPIO_5_GPIO);

 /*GPIO 设置为输出*/
 IoTGpioSetDir(LED_PIN_NAME,IOT_GPIO_DIR_OUT);
 IoTGpioSetDir(MOTOR_PIN_NAME,IOT_GPIO_DIR_OUT);
 attr.name = "ADCLightTask";
 attr.attr_bits = 0U;
 attr.cb_mem = NULL;
 attr.cb_size = 0U;
 attr.stack_mem = NULL;
 attr.stack_size = 4096;
 attr.priority = osPriorityNormal;
 if (osThreadNew(ADCLightTask, NULL, &attr) == NULL) {
 printf("[ADCLightDemo] Falied to create ADCLightTask!\n");
 }
}
SYS_RUN(ADCLightDemo);
